import 'package:flutter/services.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';

/// ------------------------------------------------------------
/// 🧩 iOS 原生交互总管理类
/// ------------------------------------------------------------
///
/// 包含：
/// 1️⃣ 屏幕保护（禁止截屏录屏）
/// 2️⃣ 设备信息与 UUID 交互
///
class IosCommunication {
  // -------------------------
  // 📡 设备通道（用于UUID、设备ID）
  // -------------------------
  static const MethodChannel _deviceChannel = MethodChannel(
    'com.example.flutter_ios_channel/method',
  );

  // -------------------------
  // ✅ 获取设备UUID（通过原生iOS获取KeyChain中UUID）
  // -------------------------
  static Future<String?> getDeviceUUID() async {
    try {
      final result = await _deviceChannel.invokeMethod<String>(
        'getDeviceUUID',
        {},
      );
      return result;
    } on PlatformException catch (e) {
      debugPrint('获取设备UUID失败: ${e.message}');
      return null;
    }
  }
}

/// ------------------------------------------------------------
/// 💻 设备信息工具类（跨平台）
/// ------------------------------------------------------------
class DeviceInfoService {
  static final DeviceInfoService _instance = DeviceInfoService._internal();
  final DeviceInfoPlugin _deviceInfoPlugin = DeviceInfoPlugin();

  String? _brand;
  String? _model;
  String? _deviceId;

  factory DeviceInfoService() => _instance;
  DeviceInfoService._internal();

  /// 初始化设备信息（自动区分平台）
  Future<void> init() async {
    if (kIsWeb) {
      final webInfo = await _deviceInfoPlugin.webBrowserInfo;
      _brand = 'Web Browser';
      _model = webInfo.browserName.name;
      _deviceId = webInfo.userAgent;
    } else if (defaultTargetPlatform == TargetPlatform.android) {
      final androidInfo = await _deviceInfoPlugin.androidInfo;
      _brand = androidInfo.brand;
      _model = androidInfo.model;

      // Android 统一通过 iOS 通道调用 KeyChain UUID（方便后端统一）
      _deviceId = await IosCommunication.getDeviceUUID();
    } else if (defaultTargetPlatform == TargetPlatform.iOS) {
      final iosInfo = await _deviceInfoPlugin.iosInfo;
      _brand = 'Apple';
      _model = iosInfo.utsname.machine;
      _deviceId = await IosCommunication.getDeviceUUID();
    } else if (defaultTargetPlatform == TargetPlatform.macOS) {
      final macInfo = await _deviceInfoPlugin.macOsInfo;
      _brand = 'Apple';
      _model = macInfo.model;
      _deviceId = macInfo.systemGUID;
    } else if (defaultTargetPlatform == TargetPlatform.windows) {
      final windowsInfo = await _deviceInfoPlugin.windowsInfo;
      _brand = 'Windows';
      _model = windowsInfo.computerName;
      _deviceId = windowsInfo.deviceId;
    } else if (defaultTargetPlatform == TargetPlatform.linux) {
      final linuxInfo = await _deviceInfoPlugin.linuxInfo;
      _brand = 'Linux';
      _model = linuxInfo.prettyName;
      _deviceId = linuxInfo.machineId;
    }
  }

  String get brand => _brand ?? 'Unknown';
  String get model => _model ?? 'Unknown';
  String get deviceId => _deviceId ?? 'Unknown';

  /// 获取设备完整信息
  Future<Map<String, String>> getDeviceInfo() async {
    if (_brand == null || _model == null || _deviceId == null) {
      await init();
    }
    return {'brand': brand, 'model': model, 'deviceId': deviceId};
  }
}
